#include<iostream>
#include"service_impl.h"
#include "tools.h"
#include <vector>

bool ServiceImpl::addDept(Department dept)
{
	vecd.push_back(dept);
	cout<<"success"<<endl;
	//dao->save(vec);
	daoo->save(vecd);
	return true;
}
bool ServiceImpl::delDept(int deptid)
{
	vector<Department>::iterator temp;
	for(temp = vecd.begin();temp != vecd.end();temp++)
	{
		if(temp->get_department_id() == deptid)
		{
			vecd.erase(temp);
			cout << "delete success"<<endl;
			//dao->save(vec);
			daoo->save(vecd);
			return true;
		}	
	}
	cout << "delete false"<<endl;
	return false;
}
bool ServiceImpl::addEmp(int idd,Employee emp)
{
	vector<Department>::iterator temp;
	for(temp = vecd.begin();temp != vecd.end();temp++)
	{
		if(temp->get_department_id() == idd)
		{
			
			temp->get_department_ems().push_back(emp);
			//dao->save(vec);
			daoo->save(vecd);
			return true;
		}	
	}
	return false;
}
bool ServiceImpl::delEmp(int id,int empid)
{
	vector<Department>::iterator temp1;
	vector<Employee>::iterator temp2;
	for(temp1 = vecd.begin();temp1 != vecd.end();temp1++)
	{
		if(temp1->get_department_id() == id)
		{
			for(temp2 = temp1->get_department_ems().begin();temp2 != temp1->get_department_ems().end();temp2++)
			{
				if(temp2->get_employee_id() == empid)
				{
					temp1->get_department_ems().erase(temp2);
					//dao->save(vec);
					daoo->save(vecd);
					return true;
				}
			}
		}	
	}
	cout << "delete false"<<endl;
	return false;
}
bool ServiceImpl::modEmp(int depid,int idd,Employee emp)
{
	vector<Department>::iterator temp1;
	vector<Employee>::iterator temp2;
	for(temp1 = vecd.begin();temp1 != vecd.end();temp1++)
	{
		if(temp1->get_department_id() == depid)
		{
			for(temp2 = temp1->get_department_ems().begin();temp2 != temp1->get_department_ems().end();temp2++)
			{
				if(temp2->get_employee_id() == idd)
				{
				temp1->get_department_ems().erase(temp2);
				temp1->get_department_ems().push_back(emp);
				//dao->save(vec);
				daoo->save(vecd);
				return true;
				}
			}
		}	
	}
	return false;
}
void ServiceImpl::listDept()
{
	for(unsigned int i=0; i<vecd.size(); i++)
	{
		cout << vecd[i].get_department_id() << " " << vecd[i].get_department_name() << " " << vecd[i].get_department_ems().size()<< endl;
	}
}
void ServiceImpl::listEmp(int deptid)
{
	vector<Department>::iterator temp;
	for(temp = vecd.begin();temp != vecd.end();temp++)
	{
		if(temp->get_department_id() == deptid)
		{
			
	for(unsigned int i =0; i<temp->get_department_ems().size();i++)
	{
		cout << temp->get_department_ems()[i].get_employee_id()<<" "<<temp->get_department_ems()[i].get_employee_name()<<" "<<
temp->get_department_ems()[i].get_employee_sex()<< " "<<temp->get_department_ems()[i].get_employee_age()<<endl;
	}
		}
	}

}
void ServiceImpl::listAllEmp()
{
	for(unsigned int i=0; i<vecd.size(); i++)
	{
		cout << vecd[i].get_department_id() << " " << vecd[i].get_department_name() << " " << vecd[i].get_department_ems().size()<< endl;
		for(unsigned int j=0; j<vecd[i].get_department_ems().size();j++)
		{
cout << vecd[i].get_department_ems()[j].get_employee_id()<<" "<<vecd[i].get_department_ems()[j].get_employee_name()<<" "<<
vecd[i].get_department_ems()[j].get_employee_sex()<< " "<<vecd[i].get_department_ems()[j].get_employee_age()<<endl;

		}
		
	}	

}
